package org.ascollada.core
{
	import org.ascollada.core.ns.collada;
	
	public class DaeElement
	{
		use namespace collada;
		
		public var document :DaeDocument;
		public var id :String;
		public var sid :String;
		public var name :String;
		
		private static var _uid :uint = 0;
		
		/**
		 * 
		 */ 
		public function DaeElement(document:DaeDocument, element:XML=null)
		{
			this.document = document;
			if(element)
				read(element);
		}
		
		/**
		 * 
		 */ 
		public function destroy():void
		{
			this.document = null;
			this.id = this.sid = this.name = null;
		}
		
		/**
		 * 
		 */ 
		public function read(element:XML):void
		{
			this.id = element.@id.toString();
			this.id = this.id && this.id.length ? this.id : "ID" + (_uid++);
			this.sid = element.@sid.toString();
			this.name = element.@name.toString();
		}
		
		/**
		 * 
		 */ 
		public function readAttribute(element:XML, name:String, stripPound:Boolean=false):String
		{
			var attr:String = element.@[name].toString();
			if(stripPound && attr.charAt(0) == "#")
				attr = attr.substr(1);
			return attr;
		}
		
		/**
		 * 
		 */ 
		public function readText(element:XML, stripPound:Boolean=false):String
		{
			var string :String = element.text().toString();
			if(stripPound && string.charAt(0) == "#")
				string = string.substr(1);
			return string;
		}
		
		/**
		 * 
		 */ 
		public function readStringArray(element:XML):Array
		{
			return element.text().toString().split(/\s+/);
		}
	}
}